package com.ygl.hbase.phoenix;

import org.apache.zookeeper.server.util.ConfigUtils;
import org.junit.After;
import org.junit.Test;

import java.sql.*;
import java.util.Properties;

/**
 * @Date 2022/1/27 13:52
 * @Created yanggld
 * @Description
 */
public class PhoenixExample {

    String url = "jdbc:phoenix:mh1,mh2,mh3";

    private Connection conn;
    private Statement stat;
    private ResultSet rs;

    public static void main(String[] args) throws Exception {
        PhoenixExample p = new PhoenixExample();
        p.initResource();
//        p.testCreateTable();
//        p.upsert();
        p.get();
    }

    public void initResource() throws Exception {
        conn = DriverManager.getConnection(url, "none", "none");
        stat = conn.createStatement();
        conn.setAutoCommit(true);
    }

    public void testCreateTable() throws SQLException {
        String sql = "create table test_phoenix_api(mykey integer not null primary key ,mycolumn varchar )";
        stat.executeUpdate(sql);
    }

    public void get() throws SQLException {
        String sql = "select * from test_phoenix_api";
        rs = stat.executeQuery(sql);
        while (rs.next()) {
            String val1 = rs.getString(1);
            String val2 = rs.getString(2);
            System.out.println(val1+":"+val2);
        }
    }

    public void upsert() throws SQLException {
        String sql1 = "upsert into test_phoenix_api values(1,'test1')";
        String sql2 = "upsert into test_phoenix_api values(2,'test2')";
        String sql3 = "upsert into test_phoenix_api values(3,'test3')";
        stat.executeUpdate(sql1);
        stat.executeUpdate(sql2);
        stat.executeUpdate(sql3);
    }

    public void delete() throws SQLException {
        String sql1 = "delete from test_phoenix_api where mykey = 1";
        stat.executeUpdate(sql1);
    }


    public void closeResource() throws SQLException {
        if (rs != null) {
            rs.close();
        }
        if (stat != null) {
            stat.close();
        }
        if (conn != null) {
            conn.close();
        }
    }
}
